import requests
import pandas as pd
C_key = "46ebfc69980aa3ef906d7e41e067a020"

def geocode(key,address,city=None,batch=None,sig=None,output=None,callback=None):
    geo_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    response = requests.get(url=geo_url,params=params)
    return response.json()

def regeo(key,location,poitype=None,radius=None,extensions=None,batch=None,roadlevel=None):
    regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
        "batch":batch,
        "roadlevel":roadlevel        
    }
    response = requests.get(url=regeo_url,params=params)
    return response.json()

def walking(key,origin,destination):
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(url=walking_url,params=params)
    return response.json()

def integrated(key,origin,destination,city,cityd,strategy=None,nightflag=None,date=None,time=None):
    integrated_url="https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params= {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd
    }
    response = requests.get(url=integrated_url,params=params)
    return response.json()

def driving(key,origin,destination):
    drive_url="https://restapi.amap.com/v3/direction/driving?parameter"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(url=drive_url,params=params)
    return response.json()

def bicycling(key,origin,destination):
    bicycle_url="https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(url=bicycle_url,params=params)
    return response.json()

def district(keywords=None,subdistrict=None):
    areas_url="https://restapi.amap.com/v3/config/district?parameters"
    params ={
        "key":C_key,
        "keywords":keywords,
        "subdistrict":"3",
    }
    r = requests.get(url=areas_url,params=params)
    return r.json()

def text(key,keywords,types,city):
    POI_url="https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "types":types,
        "city":city
    }
    r = requests.get(url=POI_url,params=params)
    return r.json()

def around(key,location):
    around_url="https://restapi.amap.com/v3/place/around?parameters"
    params_around = {
        "key":key,
        "location":location
    }
    r = requests.get(url=around_url,params=params_around)
    return r.json()

def place(key,polygon):
    polygon_url="https://restapi.amap.com/v3/place/polygon?parameters"
    parameters = {
        "key":key,
        "polygon":polygon
    }
    r = requests.get(url=polygon_url,params=parameters)
    return r.json()

def ID(key,id):
    id_url="https://restapi.amap.com/v3/place/detail?parameters"
    params = {
        "key":key,
        "id":id
    }
    r = requests.get(url=id_url,params=params)
    return r.json()

def ip(ip):
    IP_url="https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":C_key,
        "ip":ip
    }
    r = requests.get(url=IP_url,params=params)
    return r.json()

def batch(url="/v3/place/around?offset=10&page=1&key=217fc9b3f26daa32adb8c391fe0a372f&location=116.315196,39.959971&output=json&radius=100000&types=080000"):
    params = {
        "key":C_key,
    }
    body = {
        "ops":[
            {
                "url":'{}'.format(url)
            },
            {
                "url":"/v3/place/around?offset=10&page=1&key=217fc9b3f26daa32adb8c391fe0a372f&location=116.315196,39.959971&output=json&radius=100000&types=080000"
            }
        ]
    }
    r = requests.get("https://restapi.amap.com/v3/batch?",params = params,json=body)
    data = r.json()
    return data

batch()

from PIL import Image
from io import BytesIO
import requests
def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None):
    smap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
    "key":C_key,
    "location":location,
    "zoom":zoom,
    "size":size,
    "markers":markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig,
    'output':'json'
    }
    response = requests.get(smap_url,params=params)
    data = Image.open(BytesIO(response.content))
    return data

def convert(locations,coordsys=None):
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":C_key,
        "locations":locations,
        "coordsys":coordsys
    }
    r = requests.get(url,params=params)
    return r.json()

def weatherInfo(city):
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":C_key,
        "city":city
    }
    r = requests.get(url,params=params)
    return r.json()

def inputtips(keywords):
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":C_key,
        "keywords":keywords
    }
    r = requests.get(url,params)
    return r.json()

def rectangle(rectangle=str,level=None):
    url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params = {
        "key":C_key,
        "rectangle":rectangle,
        "level":level
    }
    r = requests.get(url,params=params)
    data = r.json()
    return data
def create(name="测试围栏名称",center="113.672109,23.643371",radius="1000",valid_time="2020-11-11",desc="测试围栏描述"):
    params = {
        "key":C_key,
    }
    body = {
        "name":name,
        "center":center,
        "radius":"1000",
        "enable":"true",
        "valid_time":"2020-11-11",
        "repeat":"Mon,Tues,Wed,Thur,Fri,Sat,Sun",
        "time":"00:00,11:59;13:00,20:59",
        "desc":"测试围栏描述",
        "alert_condition":"enter;leave"
    }
    r = requests.post("https://restapi.amap.com/v4/geofence/meta",params=params,json=body) # json参数添加到body体中发送
    data = r.json()
    return data

def geofence():
    parameters = {
        "key":C_key,
    }
    body = {
        "name":"中大南方",
    }
    
    r = requests.get(url="https://restapi.amap.com/v4/geofence/meta", params=parameters,json=body)
    data = r.json()
    return data

def Trajectory():
    parameters = {
        "key":C_key,
    }
    body = [
        {
            "x": 116.449429,
            "y": 40.014844,
            "sp": 4,
            "ag": 110,
            "tm": 1478831753
        }, 
        {
            "x": 116.449639,
            "y": 40.014776,
            "sp": 3,
            "ag": 110,
            "tm": 23
        }, 
        {
            "x": 116.449859,
            "y": 40.014716,
            "sp": 3,
            "ag": 111,
            "tm": 33
        },
        {
            "x": 116.450074,
            "y": 40.014658,
            "sp": 3,
            "ag": 110,
            "tm": 31
        }, 
        {
            "x": 116.450273,
            "y": 40.014598,
            "sp": 3,
            "ag": 111,
            "tm": 20
        }]
    r = requests.get("https://restapi.amap.com/v4/grasproad/driving",params=parameters,json=body)
    data = r.json()
    return data
